/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { hvigorCore } from '@ohos/hvigor';

import { TaskNames } from '../../common/task-names.js';
import { LegacyPackageHap } from '../../legacy-tasks/legacy-package-hap.js';
import { PackageHap } from '../../package-hap.js';
import { ModuleTaskService } from '../../service/module-task-service.js';
import { TargetTaskService } from '../../service/target-task-service.js';
import { SignHap } from '../../sign-hap.js';
import { AbstractModuleHookTask } from '../abstract-module-hook-task.js';

import CommonHookTask = TaskNames.CommonHookTask;

/**
 * 组装Hap打包的任务流
 *
 * @since 2022/1/20
 */
export class AssembleHap extends AbstractModuleHookTask {
  constructor(moduleService: ModuleTaskService, isFaMode: boolean) {
    super(moduleService, isFaMode, CommonHookTask.ASSEMBLE_HAP);
  }

  initTaskDepends(taskTargetService: TargetTaskService) {
    if (this.skipSignHap(taskTargetService)) {
      this.dependsOn(
        this._isFaMode
          ? `${taskTargetService.getTargetData().getTargetName()}@${LegacyPackageHap.name}`
          : `${taskTargetService.getTargetData().getTargetName()}@${PackageHap.name}`,
      );
    } else {
      this.dependsOn(`${taskTargetService.getTargetData().getTargetName()}@${SignHap.name}`);
    }
  }

  /**
   * 是否跳过生成带签名的hap的步骤
   *
   * @private
   */
  private skipSignHap(taskTargetService: TargetTaskService) {
    return taskTargetService.getBuildOption().packOptions?.buildAppSkipSignHap && hvigorCore.isCommandEntryTask(TaskNames.CommonHookTask.ASSEMBLE_APP.name);
  }
}
